package top.txl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.txl.entity.AddressBook;
import top.txl.exception.CustomerException;
import top.txl.mapper.AddressBookMapper;
import top.txl.service.AddressBookService;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void addBook(AddressBook book) {
        addressBookMapper.addBook(book);
    }

    @Override
    public List<AddressBook> getBookLists(Integer userId) {
        List<AddressBook> lists = addressBookMapper.getBookLists(userId);
        return lists;
    }

    @Override
    public void updateBook(AddressBook book) {

        if(book.getSex() != null && !book.getSex().equals(1) && !book.getSex().equals(2)){
            throw new CustomerException("sex参数错误,只能为1或2");
        }

        addressBookMapper.updateBook(book);
    }

    @Override
    public AddressBook getBookDetail(Integer userId, Integer id) {
        AddressBook book = addressBookMapper.getBookDetail(userId,id);
        return book;
    }

    @Override
    public void updateBookNotEqualThisId(AddressBook book) {
        addressBookMapper.updateBookNotEqualThisId(book);
    }

    @Override
    public List<AddressBook> getIsDefaultAddress(Integer userId) {
        List<AddressBook> lists = addressBookMapper.getIsDefaultAddress(userId);
        return lists;
    }
}
